package com.ygqh.baby.utils;

import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.http.HttpHeaders;
import com.ygqh.baby.ao.Message;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

public class IDAuthUtil {
	
	private final static String SECRET_KEY = "32CCB3AE-45D6-44D9-B6C0-AF37AF9014C6";


/*	private static YgRequestExecutor requestExecutor;

	static {
		requestExecutor = new YgRequestExecutor();
	}*/

	public static String excute(String idNo, String name) {
		String host = "https://idenauthen.market.alicloudapi.com";
		String path = "/idenAuthentication";
		String method = "POST";
		String appcode = "7e64bcc9b4cb4f7e9a35868b9ab5ae7e";
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Authorization", "APPCODE " + appcode);
		// 根据API的要求，定义相对应的Content-Type
		headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		Map<String, String> querys = new HashMap<String, String>();
		Map<String, String> bodys = new HashMap<String, String>();

		bodys.put("idNo", idNo);
		bodys.put("name", name);
		String res = null;
		try {
			/**
			* 重要提示如下:
			* HttpUtils请从
			* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
			* 下载
			*
			* 相应的依赖请参照
			* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
			*/
			HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
			System.out.println(response.toString());
			// 获取response的body
			res = EntityUtils.toString(response.getEntity());
			JSONObject parseObject = JSONObject.parseObject(res);
			System.out.println(parseObject.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;

	}

	public static Message auth(String idNo, String name) {
		String excute = excute(idNo, name);
		if (StringUtils.isBlank(excute)) {
			return Message.error("系统繁忙。请稍后重试", null);
		}
		JSONObject parseObject = JSONObject.parseObject(excute);
		String respCode = (String) parseObject.get("respCode");
		String respMessage = (String) parseObject.get("respMessage");
		if ("0000".equals(respCode)) {
			return Message.success(respMessage, null);
		}
		return Message.error(respMessage, null);
	}

	public static void auth2() {
		String host = "https://idenauthen.market.alicloudapi.com";
		String path = "/idenAuthentication";
		String method = "POST";
		String appcode = "7e64bcc9b4cb4f7e9a35868b9ab5ae7e";
		HttpHeaders headers = new HttpHeaders();
		headers.add("Authorization", "APPCODE " + appcode);
		// 根据API的要求，定义相对应的Content-Type
		headers.add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		Map<String, String> querys = new HashMap<String, String>();
		Map<String, String> bodys = new HashMap<String, String>();
		querys.put("idNo", "37040319870523073X");
		querys.put("name", "郭浩");

		try {
			//YgResponse response = requestExecutor.post(host + path, headers, bodys);
			System.out.println("");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static String decryptIdNo(String idNo) {
		if (StringUtils.isNotBlank(idNo)) {
			return ASEUtils.decrypt(idNo, SECRET_KEY);
		}
		return idNo;
	}

	public static String encryptIdNo(String idNo) {
		if (StringUtils.isNotBlank(idNo)) {
			System.out.println(idNo);
			return ASEUtils.encrypt(idNo, SECRET_KEY);
		}
		return idNo;
	}

	public static String idNoAddStar(String idNo) {
		if (StringUtils.isNotBlank(idNo)) {
			return idNo.replaceAll("(\\d{4}).*(\\w{4})", "$1******$2");
		}
		return idNo;
	}
	

	public static String getSecretKey() {
		return SECRET_KEY;
	}

}
